package com.wink.file.center.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 阿里云配置
 *
 * @author wink
 * @date 2020/11/17 17:38:36
 */
@Configuration
public class AliyunOSSConfig {

    public static String endpoint;

    public static String accessKeyId;

    public static String accessKeySecret;

    public static String bucketName;

    public static String domain;


    @Value("${aliyun.oss.endpoint:xxxxx}")
    public void setEndpoint(String endpoint) {
        AliyunOSSConfig.endpoint = endpoint;
    }

    @Value("${aliyun.oss.access-key:xxxxx}")
    public void setAccessKeyId(String accessKeyId) {
        AliyunOSSConfig.accessKeyId = accessKeyId;
    }

    @Value("${aliyun.oss.accessKeySecret:xxxxx}")
    public void setAccessKeySecret(String accessKeySecret) {
        AliyunOSSConfig.accessKeySecret = accessKeySecret;
    }

    @Value("${aliyun.oss.bucketName:xxxxx}")
    public void setBucketName(String bucketName) {
        AliyunOSSConfig.bucketName = bucketName;
    }

    @Value("${aliyun.oss.domain:xxxxx}")
    public void setDomain(String domain) {
        AliyunOSSConfig.domain = domain;
    }

    /**
     * 阿里云文件存储client
     * 只有配置了aliyun.oss.access-key才可以使用
     *
     * @ConditionalOnProperty 是spring boot控制配置bean是否创建
     */
    @Bean
    @ConditionalOnProperty(name = "aliyun.oss.access-key", matchIfMissing = true)
    public OSS ossClient() {
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

}
